using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum GuideType
{
    None,
    Rect,
    Circle,
}

[RequireComponent(typeof(CircleGuide))]
[RequireComponent(typeof(RectGuide))]
public class GuideController : MonoBehaviour,ICanvasRaycastFilter
{
    private CircleGuide circleGuide;
    private RectGuide rectGuide;
    public Material rectMat;
    public Material circleMat;

    private Image mask;

    private RectTransform target;

    private void Awake()
    {
        mask = transform.GetComponent<Image>();
        if(mask == null)
        {
            throw new System.Exception("遮罩初始化失败");
        }

        if(rectMat == null || circleMat == null) {throw new System.Exception("材质未赋值");}

        circleGuide = transform.GetComponent<CircleGuide>();
        rectGuide = transform.GetComponent<RectGuide>();
    }
    public void Guide(Canvas canvas,RectTransform target, GuideType guideType)
    {
        this.target = target;
        switch (guideType)
        {
            case GuideType.Rect:
                mask.material = rectMat;
                rectGuide.Guide(canvas, target);
                break;
            case GuideType.Circle:
                mask.material = circleMat;
                circleGuide.Guide(canvas, target);
                break;
        }
    }

    public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
    {
        if (target == null) { return false; } // 事件不会渗透 

        return !RectTransformUtility.RectangleContainsScreenPoint(target,sp,eventCamera);
    }
}
